<?php

namespace app\admin\controller\health\goods;

use app\common\controller\Backend;
use app\health\model\HealthFuluGoods;

class FuluGoods extends Backend
{
    protected $model = null;

    public function _initialize()
    {
        parent::_initialize();
        $this->model = model(HealthFuluGoods::class);
    }

    /**
     * 福禄商品列表
     * @return string|\think\response\Json
     * @throws \think\Exception
     * @throws \think\exception\DbException
     */
    public function index()
    {
        if ($this->request->isAjax()) {
            $nobuildfields = [];
            [$where, $sort, $order, $offset, $limit] = $this->custombuildparams(null, $nobuildfields);
            $params = $this->request->param();
            $where  = [];
            if (!empty($params['name'])) {
                $where['name'] = ['like', "%{$params['name']}%"];
            }
            $paginate      = HealthFuluGoods::where($where)
                ->order('id', 'desc')
                ->paginate($limit);
            $data['rows']  = $paginate->items();
            $data['total'] = $paginate->total();
            $this->success('', null, $data);
        }
        $this->assign([
            'title' => '福禄商品管理'
        ]);
        return $this->view->fetch();
    }

    /**
     * 添加福禄商品
     * @return string
     * @throws \think\Exception
     */
    public function add()
    {
        if ($this->request->isPost()) {
            $params = $this->request->post();
            $params = json_decode($params['data'], true);
            $result = $this->validate($params, [
                'name|商品名称'           => 'require',
                'remark|商品备注'         => 'max:100',
                'goods_code|商品编号'     => 'require',
                'icon|商品图标'           => 'require',
                'cost_price|采购价'       => 'require',
                'price|售价'              => 'require',
                'vip_price|会员价'        => 'require',
                'must_element|下单必要素' => 'require',
            ]);
            if (true !== $result) {
                $this->error($result);
            }
            if ($params['price'] <= $params['cost_price']) {
                $this->error('商品售价不可小于等于采购价');
            }
            if ($params['vip_price'] <= $params['cost_price']) {
                $this->error('商品会员价不可小于等于采购价');
            }
            HealthFuluGoods::create($params);
            $this->success("添加成功");
        }
        $this->assign('title', '添加福禄商品');
        return $this->view->fetch();
    }

    /**
     * 福禄商品编辑
     * @param $id
     * @return string
     * @throws \think\Exception
     * @throws \think\exception\DbException
     */
    public function edit($id = null)
    {
        $row = HealthFuluGoods::where('id', $id)->find();
        if ($this->request->isPost()) {
            $params = $this->request->post();
            $params = json_decode($params['data'], true);
            $result = $this->validate($params, [
                'name|商品名称'           => 'require',
                'remark|商品备注'         => 'max:100',
                'goods_code|商品编号'     => 'require',
                'icon|商品图标'           => 'require',
                'cost_price|采购价'       => 'require',
                'price|售价'              => 'require',
                'vip_price|会员价'        => 'require',
                'must_element|下单必要素' => 'require',
            ]);
            if (true !== $result) {
                $this->error($result);
            }
            if ($params['price'] <= $params['cost_price']) {
                $this->error('商品售价不可小于等于采购价');
            }
            if ($params['vip_price'] <= $params['cost_price']) {
                $this->error('商品会员价不可小于等于采购价');
            }
            $row->save($params);
            $this->success("保存成功");
        }
        $this->assignconfig("row", $row);
        $this->assign('title', '编辑商品');
        return $this->view->fetch();
    }
}